from multiprocessing import Pool, Manager
import os
import random
import time


# 创建文件夹 os.mkdir("new")
# os.listdir("文件夹名")#显示文件夹下所有的文件


# copy一个文件夹
def copyFileTask(name, oldFolderName, newFolderName, queue):
    fr = open(oldFolderName + "/" + name)
    fw = open(newFolderName + "/" + name, "w")

    content = fr.read()
    fw.write(content)

    fr.close()
    fw.close()

    queue.put(name)


def main():
    # 获取永远要copy的文件夹的名字
    oldFolderName = input("请输入文件夹的名字")
    newFolderName = oldFolderName + "复件"

    # 创建一个文件夹
    if (not os.path.exists(newFolderName)):
        os.mkdir(newFolderName)

    # 获取old文件夹中所有的文件名字
    fileNames = os.listdir(oldFolderName)
    print(fileNames)

    # 使用多进程的方式copy原文件夹中的所有文件到新的文件夹中
    pool = Pool(5)
    queue = Manager().Queue()

    for name in fileNames:
        pool.apply_async(copyFileTask, args=(name, oldFolderName, newFolderName, queue))

    num = 0
    allNum = len(fileNames)
    while True:
        print("---1---")
        if(num == allNum) :
            break
        temp = queue.get()
        print("---2---")
        print("正在复制...%s" % temp)
        num += 1
        copyRate = num / allNum
        print("\r copy 的进度为：%.2f%%" % (copyRate * 100), end="")
        print("num = %d, allNum = %d " %(num, allNum))

    print("\n 已完成copy")
    # pool.close()
    # pool.join()


if __name__ == "__main__":
    main()
    print("----end----")
